package com.pshcp.patient.business;

import com.external.pay.PayService;
import com.external.pay.domain.RefundDTO;
import com.external.pay.domain.RefundResDTO;
import com.framework.exception.BizException;
import com.pshcp.common.enums.OnlineBizStatusEnum;
import com.pshcp.common.enums.PayBusinessEnum;
import com.pshcp.common.enums.RegPayStatusEnum;
import com.pshcp.domain.dto.OnlineSessionDTO;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.OnlineSession;
import com.pshcp.domain.model.Patient;
import com.pshcp.domain.model.PatientOnlineMedicalRecords;
import com.pshcp.domain.model.PatientOnlineOrder;
import com.pshcp.patient.domain.onlineorder.ao.OnlineConfirmAO;
import com.pshcp.patient.tool.OnlineMedicalTools;
import com.pshcp.patient.tool.OnlineOrderTools;
import com.pshcp.service.DoctorService;
import com.pshcp.service.OnlineSessionService;
import com.pshcp.service.PatientOnlineMedicalRecordsService;
import com.pshcp.service.PatientOnlineOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 在线咨询订单业务
 */
@Service
public class OnlineOrderBusiness {

    @Autowired
    private PatientOnlineOrderService onlineOrderService;

    @Autowired
    private PatientOnlineMedicalRecordsService medicalRecordsService;

    @Autowired
    private OnlineSessionService onlineSessionService;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private PayService payService;

    /**
     * 订单创建
     * @param confirmAO
     */
    @Transactional(rollbackFor = Exception.class)
    public Long createOrder(OnlineConfirmAO confirmAO, Doctor doctor, Patient patient){
        PatientOnlineOrder onlineOrder = OnlineOrderTools.buildConfirmAO2OnlineOrder(confirmAO);
        onlineOrder.setPayFee(doctor.getOnlineFee() == null ? (new BigDecimal(0)) : doctor.getOnlineFee());
        onlineOrder.setDoctorName(doctor.getName());
        onlineOrder.setHisDoctorId(doctor.getHisDoctorId());
        onlineOrder.setPatientName(patient.getName());
        onlineOrder.setDeptName(doctor.getMainDeptName());
        onlineOrderService.insert(onlineOrder);
        //insert medical
        PatientOnlineMedicalRecords medicalRecords = OnlineMedicalTools.buildAO2Model(confirmAO, onlineOrder);
        medicalRecordsService.insert(medicalRecords);
        return onlineOrder.getId();
    }

    /**
     * 订单确认
     * @param onlineOrder
     */
    @Transactional(rollbackFor = Exception.class)
    public void enterOnlineOrder(PatientOnlineOrder onlineOrder){
        onlineOrder.setPayStatus(RegPayStatusEnum.PAY_OK.getCode());
        onlineOrderService.update(onlineOrder);
        OnlineSession onlineSession = OnlineOrderTools.buildOnlineSession(onlineOrder);
        onlineSessionService.insert(onlineSession);
        //更新医生咨询量
        doctorService.updateOnlineNumById(onlineOrder.getDoctorId());
    }

    /**
     * 订单退诊
     * @param onlineOrder
     */
    @Transactional(rollbackFor = Exception.class)
    public void refundOnlineOrder(PatientOnlineOrder onlineOrder) throws BizException {
        if(BigDecimal.ZERO.compareTo(onlineOrder.getPayFee()) >= 0){
            onlineOrder.setPayStatus(RegPayStatusEnum.REFUND_OK.getCode());
        }else{
            // 调起支付退费，定时任务补偿退费状态
            RefundDTO refundDTO = new RefundDTO();
            refundDTO.setOrderId(onlineOrder.getId());
            refundDTO.setRefundOrderId(onlineOrder.getId());
            refundDTO.setAmount(onlineOrder.getPayFee());
            refundDTO.setPayBusiness(PayBusinessEnum.ONLINE_PAY.getCode());
            RefundResDTO resDTO = payService.refund(refundDTO);
            if(resDTO.getPayStatus() != null && resDTO.getPayStatus().intValue() == 0){
                onlineOrder.setPayStatus(RegPayStatusEnum.REFUND_OK.getCode());
            }
            if(resDTO.getPayStatus() != null && resDTO.getPayStatus().intValue() == 1){
                onlineOrder.setPayStatus(RegPayStatusEnum.REFUND_FAIL.getCode());
            }
            if(resDTO.getPayStatus() != null && resDTO.getPayStatus().intValue() == 2){
                onlineOrder.setPayStatus(RegPayStatusEnum.REFUND_ING.getCode());
            }
        }
        onlineOrder.setBizStatus(OnlineBizStatusEnum.REFUND.getCode());
        onlineOrderService.update(onlineOrder);
        // update session
        OnlineSessionDTO sessionDTO = new OnlineSessionDTO();
        sessionDTO.setPatientOnlineOrderId(onlineOrder.getId());
        onlineSessionService.refundOnlineSession(sessionDTO);
    }

}
